import json
import logging

from django.shortcuts import render
from django.views import View
from django.db.models import Count

from utils.json_fun import to_json_data
from utils.res_code import Code,error_map

from aqours import models as a_models
from index import models as i_models


# logger = logging.getLogger('django')


class IndexViews(View):
    '''
        连接admin
    '''
    def get(self,request):
        return render(request,'admin/index/index.html')

class TagmanageView(View):
    '''

    Tag  保存 分类的 图片

    '''
    def get(self,request):
        tags = i_models.tb_team.objects.values('id','team_name','team_image').filter(is_delete=False)

        return render(request,'admin/tab_views/tag_manage.html',locals())

    def post(self,request):
        '''

            json 数据传输

        '''
        json_data = request.body

        if not json_data :
            return to_json_data(errno=Code.PARAMERR,errmsg=error_map[Code.PARAMERR])
        #将json 转化为dict
        dict_data = json.loads(json_data.decode('utf-8'))
        tag_name = dict_data.get('name')

        if tag_name:
            tag_tuple = i_models.tb_team.objects.get_or_create(team_name=tag_name)
            tag_instance,tag_boolean = tag_tuple
            new_tag_dict = {
                'id':tag_instance.id,
                'nmae':tag_instance.team_name,
            }
            return to_json_data(errmsg='标签创建成功',data=new_tag_dict) if tag_boolean else to_json_data(errmsg='标签已经存在',errno=Code.DATAEXIST)
        else:
            return to_json_data(errno=Code.PARAMERR,errmsg='标签名为空')

class TagEditView(View):
    '''
    /admin/.tags/<int:tag_id>
    '''
    def delete(self,request,tag_id):
        tag = i_models.tb_team.objects.only('id').filter(id=tag_id).first()
        if tag:
            tag.is_delete = True
            tag.save(update_fields=['is_delete'])
            return to_json_data(errmsg='删除标签成功')
        else:
            return to_json_data(errno=Code.PARAMERR,errmsg=error_map[Code.PARAMERR])


    def put(self,request,tag_id):
        json_data = request.body
        if not json_data:
            return to_json_data(errno=Code.PARAMERR,errmsg=error_map[Code.PARAMERR])
        #json转换
        dict_data = json.loads(json_data.decode('utf-8'))
        tag_name = dict_data.get('name')
        tag = a_models.Tag.objects.only('name').filter(id=tag_id).first()
        if tag:
            if tag_name:
                tag_name = tag_name.strip()
                if not i_models.tb_team.objects.only('id').filter(name=tag_name).first():
                    tag.name = tag_name
                    tag.save(update_fields=['name'])
                    return to_json_data(errmsg='标签更新成功')
                else:
                    return to_json_data(errno=Code.PARAMERR,errmsg='需要更新的标签已经存在!')
        else:
            return to_json_data(errno=Code.PARAMERR,errmsg="需要更新的标签不存在")



class Pictures(View):
    def get(self,request):
        banner_data = a_models.Aq_banners.objects.only('id','title','image').filter(is_delete=False)

        return render(request,'admin/pictures/pictures_manage.html',locals())

#
# class HotNewsManageView(View):
#     '''
#
#     热门更新
#
#     '''
#     def get(self,request):
#         hot_news = models.
